<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    class="flex"
                    :disabled="baTable.form.operate == 'view'"
                >
                <FormItem 
                    :label="t('xPotcheck.TCNO')" 
                    v-model="baTable.form.items!.TCNO" prop="TCNO" 
                    type="remoteSelect"
                    :input-attr="{
                        pk: 'TCNO',
                        field: 'TCNO',
                        'remote-url': '/admin/XPot/index?search[]=%7B%22field%22:%22STATUS%22,%22val%22:%22SYZ%22,%22operator%22:%22%3D%22%7D',
                    }"
                />
                <FormItem :label="t('xPotcheck.PASSED')" 
                v-model="baTable.form.items!.PASSED" 
                prop="PASSED" 
                type="select"
                :data="{ content: { 1: '通过', 0: '不通过' } }"
                 />
                <FormItem :label="t('xPotcheck.CHECKDATE')" type="date" v-model="baTable.form.items!.CHECKDATE" prop="CHECKDATE" :input-attr="{ placeholder: t('Please select field', { field: t('xPotcheck.CHECKDATE') }) }" />
                <FormItem :label="t('xPotcheck.NEXTCHECKDATE')" type="date" v-model="baTable.form.items!.NEXTCHECKDATE" prop="NEXTCHECKDATE" :input-attr="{ placeholder: t('Please select field', { field: t('xPotcheck.NEXTCHECKDATE') }) }" />
                <FormItem 
                :label="t('xPotcheck.CHECKTYPE')" 
                v-model="baTable.form.items!.CHECKTYPE" 
                prop="CHECKTYPE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'code',
                    field: 'codename',
                    'remote-url': codelib + 'now?where=gxnjtype',
                }"
                />
                <FormItem 
                :label="t('xPotcheck.NEXTCHECKTYPE')" 
                v-model="baTable.form.items!.NEXTCHECKTYPE" 
                prop="NEXTCHECKTYPE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'code',
                    field: 'codename',
                    'remote-url': codelib + 'now?where=gxnjtype',
                }" 
                />
                <FormItem :label="t('xPotcheck.OP')" type="string" v-model="baTable.form.items!.OP" prop="OP" :input-attr="{ placeholder: '当前登录人',disabled:true }" />
                <FormItem :label="t('xPotcheck.OPTIME')" type="datetime" v-model="baTable.form.items!.OPTIME" prop="OPTIME" :input-attr="{ placeholder: '当前日期',disabled:true }" />
                <FormItem style="width:100%" :label="t('xPotcheck.DEMO')" type="textarea" v-model="baTable.form.items!.DEMO" prop="DEMO" :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('xPotcheck.DEMO') }) }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" />
                <FormItem style="width:100%" :label="t('xPotcheck.FINISHFILE')" type="file" v-model="baTable.form.items!.FINISHFILE" prop="FINISHFILE" />
                <FormItem :label="t('xPotcheck.UPLOADER')" type="string" v-model="baTable.form.items!.UPLOADER" prop="UPLOADER" :input-attr="{ placeholder: '上传的用户',disabled:true }" />
                <FormItem :label="t('xPotcheck.UPLOADTIME')" type="datetime" v-model="baTable.form.items!.UPLOADTIME" prop="UPLOADTIME" :input-attr="{ placeholder:'上传的时间',disabled:true}" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate !== 'view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, onMounted, watchEffect } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { codelib } from '/@/api/controllerUrls'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({

    TCNO: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xPotcheck.TCNO') })})],
    CHECKDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotcheck.CHECKDATE') })})],
    CHECKTYPE: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xPotcheck.CHECKTYPE') })})],
    NEXTCHECKDATE: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xPotcheck.NEXTCHECKDATE') })})],
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotcheck.OPTIME') })})],
    UPLOADTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotcheck.UPLOADTIME') })})],
})
</script>

<style scoped lang="scss"></style>
